//
//  Driving_ViewController.m
//  duoduo
//
//  Created by wanghao on 16/10/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "Driving_ViewController.h"
#import "LXMapView.h"
#import "DrivingListTableVC.h"
#import "DrivingInfo_ViewController.h"

@interface Driving_ViewController ()
{
    LXMapView * homeMap_;
    UIButton *btn;
    NSInteger count;
    UIView * allInView;
}
@end

@implementation Driving_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"自驾车";
    count = 0;

    [self setRightItem];
    [self initUI];

}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

//右侧item初始化
-(void)setRightItem
{
    btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];


    //  [btn setImage:[UIImage imageNamed:@"top_navigation_infoicon"] forState:UIControlStateNormal];
    [btn setTitle:@"深圳" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(2, 12, 0, -12)];
    [btn setImage:[UIImage imageNamed:@"ditu"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"ditu"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}

//初始化自驾车列表
- (void)initUI
{
    allInView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    allInView.backgroundColor = [UIColor whiteColor];

    DrivingListTableVC * vc = [[DrivingListTableVC alloc]init];
    vc.tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-114);
    [self.view addSubview:allInView];
    [self addChildViewController:vc];
    [allInView addSubview:vc.tableView];
}

//初始化地图
- (void)setMapUI
{
    LXMapView * map = [[LXMapView alloc]initWithMapFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self.view addSubview:map];
    map.index = 1;
    homeMap_ = map;

    map.jumpToInfoBlock = ^()
    {
        DrivingInfo_ViewController * vc = [[DrivingInfo_ViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    };
}

//切换视图
- (void)clickRightItem:(UIButton *)sender
{
    count++;
    DrivingListTableVC * vc = [[DrivingListTableVC alloc]init];
    vc.tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-114);
    __weak __typeof(self)weakSelf = self;
    vc.drivingJumpBlock = ^()
    {
        DrivingInfo_ViewController * drivingVC = [[DrivingInfo_ViewController alloc]init];
        [weakSelf.navigationController pushViewController:drivingVC animated:YES];
    };

    if (count % 2 == 1) {
        allInView.alpha = 0;
        [btn setTitle:@"列表" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"liebiao"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"liebiao"] forState:UIControlStateSelected];

        [vc removeFromParentViewController];
        [vc.tableView removeFromSuperview];
        [self setMapUI];

    }else{
        allInView.alpha = 1;
        [btn setTitle:@"深圳" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ditu"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ditu"] forState:UIControlStateSelected];
        [self.view addSubview:allInView];
        [self addChildViewController:vc];
        [allInView addSubview:vc.tableView];
    }

}



@end
